package com.wanmait.recruitment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruitment.mapper.FriendlyMapper;
import com.wanmait.recruitment.pojo.Friendly;
import com.wanmait.recruitment.service.FriendlyService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class FriendlyServiceImpl implements FriendlyService {
    @Resource
    private FriendlyMapper friendlyMapper;
    @Override
    @CacheEvict(value = "friendlyCache",allEntries = true)
    public void insert(Friendly object) {
        friendlyMapper.insertSelective(object);
    }

    @Override
    @CacheEvict(value = "friendlyCache",allEntries = true)
    public void delete(Integer id) {
        friendlyMapper.del(id);
    }

    @Override
    @CacheEvict(value = "friendlyCache",allEntries = true)
    public void delete(String ids) {
        String idArr[] = ids.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i=0;i<idArr.length;i++){
            list.add(Integer.parseInt(idArr[i]));
        }
        friendlyMapper.batchDel(list);
    }

    @Override
    @CacheEvict(value = "friendlyCache",allEntries = true)
    public void update(Friendly object) {
        friendlyMapper.updateByPrimaryKeySelective(object);
    }

    @Override
    public Friendly findById(Integer id) {
        return null;
    }

    @Override
    @Cacheable("friendlyCache")
    public List<Friendly> findAll() {
        return friendlyMapper.selectList();
    }

    @Override
    public List<Friendly> findAll(Friendly object) {
        return null;
    }

    @Override
    public PageInfo<Friendly> findPage(int pageNum, int pageSize) {
        return null;
    }

    @Override
    @Cacheable("friendlyCache")
    public PageInfo<Friendly> findList(Friendly friendly, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Friendly> friendlies = friendlyMapper.select(friendly);
        PageInfo<Friendly> pageInfo = new PageInfo<>(friendlies);
        return pageInfo;
    }
}
